package store.lunangangster.nodeflow.core.component;

import com.alibaba.ttl.TransmittableThreadLocal;
import store.lunangangster.nodeflow.enums.NodeType;
import store.lunangangster.nodeflow.util.KeyGenerator;

/**
 * <h2>条件Node</h2>
 *
 * @author cwk
 * @version 1.0
 */
public abstract class IfComponent extends NodeComponent {

	private static final ThreadLocal<String> RESULT_KEY_TL = new TransmittableThreadLocal<>();

	@Override
	public void process() {
		boolean result = this.processIf();
		// 根据result结果进行后续流程
		String key = KeyGenerator.generate(NodeType.NODE_IF);
		RESULT_KEY_TL.set(key);
		this.getSlot().setData(key, result);
	}

	/**
	 * IfComponent的执行逻辑
	 * @return 判断选择true分支或false分支
	 */
	protected abstract boolean processIf();

	@Override
	public String getIfResultKey() {
		return RESULT_KEY_TL.get();
	}

	@Override
	public void removeIfResultKey() {
		RESULT_KEY_TL.remove();
	}
}
